<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>解构赋值(destructuring assignment)</title>
    <script>
      //数组赋值
      let [a, b, c] = [10, 20, 30];
      console.log(a, b, c); //10,20,30

      let [x, y, ...other] = [1, 2, 3, 4, 5];
      console.log(x, y, other); //1 2 [3, 4, 5]
      //对象赋值
      let { name, age } = {
        name: "lmx",
        age: 21,
      };
      console.log(name, age); //lmx 21
      //函数赋值
      function fn() {
        return [10, 20];
      }
      let [num1, num2] = fn();
      console.log(num1, num2);
      //函数参数名指定
      function fn2({ x = 1, y = 2 }) {
        return x + y;
      }
      console.log(fn2({})); // 3
      console.log(fn2({ x: 2 })); // 4
      console.log(fn2({ y: 1 })); // 2
      console.log(fn2({ x: 3, y: 3 })); // 6
      //必须指定参数
      function required() {
        throw new Error("参数未制定");
      }
      function sayBye(name = required()) {
        console.log(`${name} bye!`);
      }
      sayBye("lmx"); // lmxbye
      sayBye(); //抛出异常Error:参数未制定
    </script>
  </head>
  <body></body>
</html>
